package main

import (
	"im/ctrl"
	"log"
	"net/http"
	"text/template"
)

// 注册视图
func RegisterView() {
	tpl, err := template.ParseGlob("view/**/*")
	if err != nil {
		log.Fatal(err)
	}
	for _, v := range tpl.Templates() {
        tplName := v.Name()
		http.HandleFunc(tplName, func(w http.ResponseWriter, r *http.Request) {
			tpl.ExecuteTemplate(w, tplName, nil)
		})
	}
}

func main() {
	// 路由
	http.HandleFunc("/user/login", ctrl.UserLogin)
	http.HandleFunc("/user/register", ctrl.UserRegister)
	http.HandleFunc("/contact/add", ctrl.AddFriend)
	http.HandleFunc("/contact/friend", ctrl.LoadFriend)
    http.HandleFunc("/chat", ctrl.Chat)
    http.HandleFunc("/upload", ctrl.Upload)
	// http.HandleFunc("/contact/community", ctrl.LoadCommunity)
	// http.HandleFunc("/contact/join", ctrl.JoinCommunity)
	// 静态文件
	// http.Handle("/", http.FileServer(http.Dir(".")))
	http.Handle("/asset/", http.FileServer(http.Dir(".")))
	http.Handle("/mnt/", http.FileServer(http.Dir(".")))
	RegisterView()
    // 启动服务并监听
	http.ListenAndServe(":8080", nil)
}
